﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Controls;
using Xceed.Silverlight.Compatibility;

namespace Xceed.Silverlight.Controls
{
  #region Metadata

  [TemplateVisualState( GroupName = "WatermarkStates", Name = "WatermarkVisible" )]
  [TemplateVisualState( GroupName = "WatermarkStates", Name = "WatermarkHidden" )]

  [TemplatePart( Name = "WatermarkPresenter", Type = typeof( ContentPresenter ) )]

  #endregion

  public class WatermarkTextBox : AutoSelectTextBox
  {
    public WatermarkTextBox()
    {
      m_hasFocus = false;
      this.DefaultStyleKey = typeof( WatermarkTextBox );
    }

    #region WatermarkContentTemplate Property

    public static readonly DependencyProperty WatermarkContentTemplateProperty = DependencyProperty.Register(
      "WatermarkContentTemplate",
      typeof( DataTemplate ),
      typeof( WatermarkTextBox ),
      new PropertyMetadata(
        ( DataTemplate )null ) );

    public DataTemplate WatermarkContentTemplate
    {
      get
      {
        return ( DataTemplate )this.GetValue( WatermarkTextBox.WatermarkContentTemplateProperty );
      }
      set
      {
        this.SetValue( WatermarkTextBox.WatermarkContentTemplateProperty, value );
      }
    }

    #endregion

    #region WatermarkContent Property

    public static readonly DependencyProperty WatermarkContentProperty = DependencyProperty.Register(
      "WatermarkContent",
      typeof( object ),
      typeof( WatermarkTextBox ),
      new PropertyMetadata(
        ( object )null ) );

    public object WatermarkContent
    {
      get
      {
        return ( object )this.GetValue( WatermarkTextBox.WatermarkContentProperty );
      }
      set
      {
        this.SetValue( WatermarkTextBox.WatermarkContentProperty, value );
      }
    }

    #endregion

    #region HasFocus Property

    private bool HasFocus
    {
      get
      {
        return m_hasFocus;
      }
      set
      {
        if( m_hasFocus != value )
        {
          m_hasFocus = value;
          this.ChangeVisualState();
        }
      }
    }

    #endregion

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();

      ContentPresenter watermarkPresenter = ( ContentPresenter )this.GetTemplateChild( "WatermarkPresenter" );

      if( watermarkPresenter != null )
      {
        m_watermarkPresenter = watermarkPresenter;
      }

      this.ChangeVisualState( false );
    }

    protected override void OnTextChanged( object sender, TextChangedEventArgs e )
    {
      base.OnTextChanged( sender, e );
      this.ChangeVisualState();
    }

    protected override void OnGotFocus( RoutedEventArgs e )
    {
      base.OnGotFocus( e );
      this.HasFocus = true;
    }

    protected override void OnLostFocus( RoutedEventArgs e )
    {
      base.OnLostFocus( e );
      this.HasFocus = false;
    }

    protected override void ChangeVisualStateCore( bool useTransitions )
    {
      base.ChangeVisualStateCore( useTransitions );

      this.UpdateWatermarkStates( useTransitions );
    }

    private void UpdateWatermarkStates( bool useTransitions )
    {
      if( ( !this.HasFocus ) &&
          ( ( string.IsNullOrEmpty( this.Text ) ) || ( StringHelper.IsNullOrWhiteSpace( this.Text ) ) ) )
      {
        VisualStateManager.GoToState( this, "WatermarkVisible", useTransitions );
      }
      else
      {
        VisualStateManager.GoToState( this, "WatermarkHidden", useTransitions );
      }
    }

    private ContentPresenter m_watermarkPresenter;
    private bool m_hasFocus;
  }
}
